package com.blog.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.entity.SysMenu;
import com.blog.entity.SysUser;
import com.blog.mapper.SysMenuMapper;
import com.blog.service.SysMenuService;
import com.blog.util.StringUtil;

import com.blog.util.dto.AjaxResult;
import com.blog.util.tree.TreeUtil;
import com.blog.util.tree.XmTreeVo;
import com.blog.vo.menu.SaveMenuVo;
import com.blog.vo.menu.SysMainMenuVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {


    @Resource
    SysMenuMapper menuMapper;

    @Override
    public List<SysMainMenuVo> getMenuInfo(String userId) {
        List<SysMainMenuVo> list = menuMapper.getMenuListById(userId);
        return list.stream().filter(m -> m.getParentId() == 0).map(
                (m) -> {
                    m.setChild(getChildrenList(m, list));
                    return m;
                }
        ).collect(Collectors.toList());
    }

    private List<SysMainMenuVo> getChildrenList(SysMainMenuVo root, List<SysMainMenuVo> all) {
        return all.stream().filter(m -> {
            return Objects.equals(m.getParentId(), root.getId());
        }).map(
                (m) -> {
                    m.setChild(getChildrenList(m, all));
                    return m;
                }
        ).collect(Collectors.toList());
    }


    @Override
    public List<Map<String, Object>> tableList() {
        return menuMapper.tableList();
    }

    @Override
    public List<XmTreeVo> queryMenuXmTree(List<Long> menuIds) {
        List<XmTreeVo> list = menuMapper.queryMenuXmTree();
        if (menuIds != null && menuIds.size() > 0){
            TreeUtil.setXmTreeSelected(list,menuIds);
        }else {
            TreeUtil.setXmTreeSelected(list, new ArrayList<>(Collections.singletonList(1L)));
        }
        return TreeUtil.getXmTree(list);
    }


    @Override
    public AjaxResult saveMenu(SaveMenuVo menuVo) {
        if (menuVo.getParentId() == null){
            return AjaxResult.error("父级目录不能为空");
        }
        if (StringUtil.isBlank(menuVo.getTitle())){
            return AjaxResult.error("菜单名称不能为空");
        }

        Subject subject = SecurityUtils.getSubject();
        SysUser user = (SysUser) subject.getPrincipal();

        SysMenu menu = new SysMenu();
        QueryWrapper<SysMenu> maxIdWrapper = new QueryWrapper<>();
        maxIdWrapper.eq("menu_type",menuVo.getMenuType()).orderByDesc("id").last("LIMIT 1");
        SysMenu one = this.getOne(maxIdWrapper);
        Long id = one.getId()+1L;

        QueryWrapper<SysMenu> maxOrderNumWrapper = new QueryWrapper<>();

        maxOrderNumWrapper.orderByDesc("order_num").last("limit 1");
        SysMenu maxOrder = this.getOne(maxOrderNumWrapper);
        Integer maxOrderNum = maxOrder.getOrderNum() + 1;

        BeanUtils.copyProperties(menuVo,menu);
        menu.setId(id);
        menu.setOrderNum(maxOrderNum);
        menu.setCreateBy(user.getId());
        menu.setTarget("_self");
        if ("M".equals(menu.getMenuType())){
            menu.setHref("");
        }
        if ("F".equals(menu.getMenuType())){
            menu.setIcon("#");
            menu.setHref("#");
            menu.setTarget("#");
        }

        this.save(menu);
        return AjaxResult.success();
    }
}